/*
 * @Author: error: git config user.name && git config user.email & please set dead value or install git
 * @Date: 2022-10-19 16:50:34
 * @LastEditors: error: git config user.name && git config user.email & please set dead value or install git
 * @LastEditTime: 2022-10-20 14:14:00
 * @FilePath: \作业\routes\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
var express = require('express');
var router = express.Router();

var db = require('../model/db')

/* GET home page. */
// router.get('/', function (req, res, next) {
//   res.render('index', { title: 'Express' });
// });

//渲染首页
router.get('/index', (req, res) => {
  res.render('index.html')
})

//首页ajax数据后台
router.get("/sign", async (req, res) => {
  let sql = `select * from stu_score`;
  let [err, data] = await db.query(sql);
  if (err) {
    res.send({ "code": "400", "mag": "查询失败" })
  } else {
    res.send([data])
  }
})

router.get('/add', (req, res) => {
  res.render('add.html')
})

//添加页ajax数据后台
router.post('/add', async (req, res) => {
  let { name, sex, age } = req.body;
  console.log(name, sex, age)
  let sql = `insert into stu_score (name,sex,age) value("${name}","${sex}",${age})`
  let [err, data] = await db.query(sql);
  if (err) {
    res.send({ "code": "400", "msg": "添加失败" })
  } else {
    res.send({ "code": "200", "msg": "添加成功" })
  }
})
module.exports = router;
